import os
import signal
import subprocess

from settings import settings


class Killer:
    __slots__ = []

    def __init__(self) -> None:
        pass

    def make_action_to_service(self, name: str, action: str) -> None:
        self.update_pid_list()
        pid = settings.PIDS_SERVICES.get(name)
        match action:
            case 'on':
                self.start_service(name)
            case 'off':
                settings.SERVICES_NAME.remove(name)
                settings.PID_LIST.remove(pid)
                del settings.PIDS_SERVICES[name]
                os.kill(pid, signal.SIGTERM)
            case 'reboot':
                os.kill(pid, signal.SIGTERM)
                self.start_service(name)

    @staticmethod
    def start_service(name) -> None:
        script_path = settings.APP_PATH + f"/services/{name}/run.py"
        path = subprocess.getoutput('which python3.10')
        subprocess.Popen([path, script_path])

    @staticmethod
    def update_pid_list() -> None:
        pids_services: dict = dict(zip(settings.SERVICES_NAME, settings.PID_LIST))
        settings.PIDS_SERVICES = pids_services
